package com.ym.b2b.tags;

import javax.servlet.jsp.JspTagException;

import org.apache.commons.lang.StringUtils;

import com.ym.b2b.news.service.EmBNewsService;
import com.ym.b2b.news.sqlbean.EmBNews;
import com.ym.b2b.tags.base.BaseTag;
import com.zc.commons.base.Result;
import com.zc.commons.spring.SpringContextHolder;

/**
 * @ClassName: NewsListTags
 * @Description: 获取新闻简易列表标签
 * @Author 竺夏军
 * @Date 2015年1月28日 上午12:02:54
 * @Modify
 * @CopyRight 信息化团队
 */
public class NewsTags extends BaseTag {

    private Integer           isIndex;
    private Integer           isTop;
    private Integer           isCommend;
    private Integer			  isScroll;	//	是否滚动飘窗
    private Integer           limitRow;
    protected String          newsTypeName;
    protected Long            newsTypeId;
    protected String          getType;
    protected Long            newsId;
    protected String          orderBy;

    private static final long serialVersionUID = 895777383599438520L;

    public int doStartTag() throws JspTagException {
        Result ret = new Result();
        if (StringUtils.isBlank(getGetType())) {
            return EVAL_BODY_INCLUDE;
        }
        if (getGetType().equals("list")) {
            EmBNews emBNews = new EmBNews();
            emBNews.setNewsStatus(1);
            emBNews.setLimitRows(getLimitRow());
            emBNews.setIsIndex(getIsIndex());
            emBNews.setIsTop(getIsTop());
            emBNews.setNewsTypeId(getNewsTypeId());
            emBNews.setIsCommend(getIsCommend());
            emBNews.setIsScroll(getIsScroll());
            emBNews.setNewsTypeName(getNewsTypeName());
            emBNews.setOrderBy(getOrderBy());
            // 设置缓存
            setCache(emBNews);
            EmBNewsService emBNewsService = SpringContextHolder.getBean("emBNewsService");
            ret = emBNewsService.searchSimpleNewsList(emBNews);
            getPageContext().getRequest().setAttribute(getOutResponseAttrName(), ret);
        }
        if (getGetType().equals("detail")) {
            EmBNews emBNews = new EmBNews();
            emBNews.setNewsId(getNewsId());

            emBNews.setNewsStatus(1);
            EmBNewsService emBNewsService = SpringContextHolder.getBean("emBNewsService");
            emBNews = emBNewsService.searchEmBNews(emBNews);
            getPageContext().getRequest().setAttribute(getOutResponseAttrName(), emBNews);
        }
        return EVAL_BODY_INCLUDE;
    }

    public int doEndTag() throws JspTagException {

        return EVAL_BODY_INCLUDE;
    }

    private void setCache(EmBNews emBNews) {

        String cacheKey = "key_";
        if (null != getLimitRow()) {
            cacheKey += getLimitRow();
        }
        if (null != getIsIndex()) {
            cacheKey += getIsIndex();
        }
        if (null != getIsTop()) {
            cacheKey += getIsTop();
        }
        if (null != getNewsTypeId()) {
            cacheKey += getNewsTypeId();
        }
        if (null != getIsCommend()) {
            cacheKey += getIsCommend();
        }
        if (null != getNewsTypeName()) {
            cacheKey += getNewsTypeName();
        }
        if (null != getOrderBy()) {
            cacheKey += getOrderBy();
        }
        if (null != getIsScroll()) {
            cacheKey += getIsScroll();
        }
        emBNews.setSearchFlag("tag");
        emBNews.setCacheKey(cacheKey);

    }

    public Integer getIsIndex() {
        return isIndex;
    }

    public void setIsIndex(Integer isIndex) {
        this.isIndex = isIndex;
    }

    public Integer getIsTop() {
        return isTop;
    }

    public void setIsTop(Integer isTop) {
        this.isTop = isTop;
    }

    public Integer getIsCommend() {
        return isCommend;
    }

    public Integer getIsScroll() {
		return isScroll;
	}

	public void setIsScroll(Integer isScroll) {
		this.isScroll = isScroll;
	}

	public void setIsCommend(Integer isCommend) {
        this.isCommend = isCommend;
    }

    public Integer getLimitRow() {
        return limitRow;
    }

    public void setLimitRow(Integer limitRow) {
        this.limitRow = limitRow;
    }

    public String getNewsTypeName() {
        return newsTypeName;
    }

    public void setNewsTypeName(String newsTypeName) {
        this.newsTypeName = newsTypeName;
    }

    public Long getNewsTypeId() {
        return newsTypeId;
    }

    public void setNewsTypeId(Long newsTypeId) {
        this.newsTypeId = newsTypeId;
    }

    public String getGetType() {
        return getType;
    }

    public void setGetType(String getType) {
        this.getType = getType;
    }

    public Long getNewsId() {
        return newsId;
    }

    public void setNewsId(Long newsId) {
        this.newsId = newsId;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

}
